package io.renren.modules.student.controller;

import io.renren.common.enums.PunchStatus;
import io.renren.common.exception.RRException;
import io.renren.common.utils.IPUtil;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.student.entity.PunchEntity;
import io.renren.modules.student.service.PunchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;


/**
 * @author guijing
 * @email sunlightcs@gmail.com
 * @date 2022-02-24 21:39:53
 */
@RestController
@RequestMapping("student/punch")
@Transactional
@Slf4j
public class PunchController {
    @Autowired
    private PunchService punchService;

    /**
     * 学生个人打卡记录列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        Long userId = ShiroUtils.getUserId();
        params.put("userId", userId);
        PageUtils page = punchService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/getPunch")
    public R getPunchStatus() {
        Long userId = ShiroUtils.getUserId();
        long millis = System.currentTimeMillis();
        java.sql.Date date = new java.sql.Date(millis);
        PunchEntity punchEntity = new PunchEntity();
        punchEntity.setPunchDate(date);
        punchEntity.setUserId(userId);
        PunchEntity todayPunch = punchService.getTodayPunch(punchEntity);
        if (Objects.isNull(todayPunch)) {
            punchEntity.setPunchStatus(PunchStatus.NOPUNCH.getLevel());
            punchService.save(punchEntity);
            return R.ok().put("punch", punchEntity);
        }


        return R.ok().put("punch", todayPunch);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{punchId}")
    public R info(@PathVariable("punchId") Long punchId) {
        PunchEntity punch = punchService.getById(punchId);

        return R.ok().put("punch", punch);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PunchEntity punchEntity, HttpServletRequest request) {
        java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
        punchEntity.setPunchDate(date);
        punchEntity.setUserId(ShiroUtils.getUserId());
        PunchEntity todayPunch = punchService.getTodayPunch(punchEntity);
        if (Objects.isNull(todayPunch)) {
            throw new RRException("今日打卡时间已过");
        }
        punchEntity.setPunchId(todayPunch.getPunchId());
        punchEntity.setPunchTime(new java.util.Date());
        punchEntity.setPunchStatus(PunchStatus.ISPUNCH.getLevel());
        //获取请求所在ip位置
        String ipAddr = IPUtil.getIpAddr(request);
        try {
            String cityInfo = IPUtil.getCityInfo(ipAddr);
            punchEntity.setPunchLocation(cityInfo);
            punchService.updateById(punchEntity);
        } catch (Exception e) {
            log.error("获取打卡所在城市失败", e);
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PunchEntity punch) {
        punchService.updateById(punch);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] punchIds) {
        punchService.removeByIds(Arrays.asList(punchIds));

        return R.ok();
    }

}
